﻿using System;
using System.Collections;
using System.Configuration;
using System.Data; 
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity; 

namespace Runda.CMS.Portal.ContractManage
{
    public partial class ContractProfitSale : BasePage
    {
        public Guid ContractID
        {
            get {
                if (Request.QueryString["ContractID"] != null)
                {
                    return new Guid(Request.QueryString["ContractID"].ToString());
                }
                else
                    return Guid.Empty;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["ContractID"] != null)
                {
                    InitData();
                }
            }
        }

        /// <summary>
        /// 保存资金成本
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            decimal CapitalCost = 0, OtherCost = 0;
            if (radtxtCapitalCost.Value.HasValue)
                CapitalCost = (decimal)radtxtCapitalCost.Value.Value;

            if (radtxtOthorCost.Value.HasValue)
                OtherCost = (decimal)radtxtOthorCost.Value.Value;

            ContractBll bll = new ContractBll();
            CtrtContractEntity entity = bll.Get<CtrtContractEntity>(new Guid(Request.QueryString["ContractID"]));
         
            entity.CapitalCost = CapitalCost;
            entity.OtherCost = OtherCost;

            bll.Update(entity);

            InitData();
        }

        private void InitData()
        {
            ContractCalculateBll bll = new ContractCalculateBll();

            #region 成本

            //合同销售商品成本
            decimal ContractItemCost = bll.SumContractItemCost(ContractID);
            ContractItemCost = decimal.Round(ContractItemCost, 2);
            lblContractItemCost.Text = ContractItemCost.ToString("N");

            //配套商品成本
            decimal ContractGiftCost = 0;
            ContractGiftCost = bll.GetContractGiftCost(ContractID);
            lblContractGiftCost.Text = ContractGiftCost.ToString("N");

            //保修成本 
            decimal PartCost = 0;
            PartCost = bll.GetContractItemPartCost(ContractID);
            lblContractPartCost.Text = PartCost.ToString("N");
            //资金成本
            decimal CapitalCost = 0;
            CtrtContractEntity entity = bll.Get<CtrtContractEntity>(ContractID);
            CapitalCost = entity.CapitalCost;
            radtxtCapitalCost.Value = (double)CapitalCost;
            //其他成本
            decimal OtherCost = 0;
            OtherCost = entity.OtherCost;
            radtxtOthorCost.Value = (double)OtherCost;
            //成本合计
            decimal TotalCost = (ContractItemCost + ContractGiftCost + PartCost + CapitalCost+OtherCost);
            lblTotalCost.Text = TotalCost.ToString();
            #endregion

            //销售收入
            decimal TotalPrice = 0;
            TotalPrice = bll.SumContractSaleItemPrice(ContractID);
            lblContractTotalPrice.Text = TotalPrice.ToString("N");

            CalculateProfit(TotalPrice, TotalCost);
        }

        /// <summary>
        /// 计算利润
        /// </summary>
        /// <param name="Price"></param>
        /// <param name="Cost"></param>
        private void CalculateProfit(decimal Price, decimal Cost)
        {
            //利润率
            lblProfit.Text = (Price - Cost).ToString("N");

            if (Price != 0)
                lblProfitRate.Text = ((Price - Cost)*100 / Price).ToString("N")+"%";
        }

    }
}
